package pt.mauromad.guias.controller;

import static java.lang.String.format;
import static pt.mauromad.guias.utils.transformation.Transformations.Manifest2Bean;
import static pt.mauromad.guias.utils.transformation.Transformations.Manifest2Dao;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pt.mauromad.guias.bean.ManifestBean;
import pt.mauromad.guias.dao.AddressDao;
import pt.mauromad.guias.dao.manifest.ManifestDao;
import pt.mauromad.guias.refdata.DocumentType;
import pt.mauromad.guias.utils.form.Action;

@Controller
@RequestMapping("/manifest")
public class ManifestController extends BaseController {
	@RequestMapping(value = "")
	public String listByUser(Model model) {
		model.addAttribute("manifests", manifestService.byUser(getUser().getId()));
		return "list/all-manifests";
	}

	@RequestMapping(value = "all")
	public String listManifests(Model model) {
		model.addAttribute("manifests", manifestService.all());
		return "list/all-manifests";
	}

	@RequestMapping(value = "/{manifest:[0-9]+}", method = RequestMethod.GET)
	public String show(Model model, @PathVariable ManifestDao manifest) {
		model.addAttribute("form", Manifest2Bean.get(manifest));
		model.addAttribute("allowDelete", true);
		add(model, Action.createPost("manage"));
		return "manifest-form";
	}

	@RequestMapping(value = "/{manifest:[0-9]+}", method = RequestMethod.POST, params = "action=manage")
	public String edit(Model model, @PathVariable ManifestDao manifest) {
		model.addAttribute("form", manifest);
		model.addAttribute("documentTypes", DocumentType.values());
		model.addAttribute("editable", true);
		add(model, Action.createPost("manage.save"));
		add(model, Action.createPost("delete"));
		return "manifest-form";
	}

	@RequestMapping(value = "/{manifest:[0-9]+}", method = RequestMethod.POST, params = "action=manage.save")
	public String editManifest(Model model, @Validated ManifestBean bean, @PathVariable Long manifest) {
		ManifestDao edited = Manifest2Dao.get(bean);
		edited.setId(manifest);
		manifestService.save(edited);
		return format("redirect:/manifest/%s", manifest);
	}

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public String create(Model model) {
		model.addAttribute("editable", true);
		model.addAttribute("documentTypes", DocumentType.values());
		List<AddressDao> addresses = addressService.byAccount(getAccount().getId(), true);
		model.addAttribute("originAddressList", addresses);
		model.addAttribute("destinationAddressList", addresses);
		model.addAttribute("shippingAddressList", addresses);
		model.addAttribute("deliveryAddressList", addresses);
		add(model, Action.createPost("save"));
		return "manifest-form";
	}

	@RequestMapping(value = "/new", method = RequestMethod.POST)
	public String createManifest(Model model, @Validated ManifestBean form) {
		ManifestDao dao = Manifest2Dao.get(form);
		dao.setOwner(getUser());
		manifestService.save(dao);
		return format("redirect:/manifest/%s", dao.getId());
	}

	@RequestMapping(value = "/{manifest:[0-9]+}", method = RequestMethod.POST, params = "delete")
	public String deleteConfirm(Model model, @PathVariable ManifestDao manifest) {
		model.addAttribute("form", manifest);
		add(model, Action.createPost("delete.confirm"));
		return "common/confirm-delete";
	}

	@RequestMapping(value = "/{manifest:[0-9]+}", method = RequestMethod.POST, params = "delete.confirm")
	public String delete(Model model, @PathVariable Long manifest) {
		manifestService.delete(manifest);
		return format("redirect:/manifest/all");
	}
}
